package com.beidouapp.dao.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.beidouapp.dao.UserDao;
import com.beidouapp.domain.example.UserExample;
import com.beidouapp.domain.example.UserExample.Criteria;
import com.beidouapp.domain.pojo.User;
import com.beidouapp.mapper.BaseMapper;
import com.beidouapp.mapper.UserMapper;

@Component
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {

    @Autowired
    private UserMapper userMapper;

    public BaseMapper<User> getResourceMapper() {
        return userMapper;
    }

    @Override
    public int insert(User record) {
        return userMapper.addEntry(record);
    }

    @Override
    public User findUserById(Long id) {
        UserExample example = new UserExample();
        Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        List<User> list = userMapper.selectByExample(example);
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public User findUserByEmpId(Long empId) {
        UserExample example = new UserExample();
        Criteria criteria = example.createCriteria();
        criteria.andEmpIdEqualTo(empId);
        List<User> list = userMapper.selectByExample(example);
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public User getUserByLoginName(String loginName) {
        User u =userMapper.getUserByLoginName(loginName);
        return u;
    }
    
    @Override
    public User getUserWithPSWByLoginName(String loginName) {
        User u =userMapper.getUserWithPSWByLoginName(loginName);
        return u;
    }
    

    @Override
    public int removeUserById(Long id) {
        User user = new User();
        user.setId(id);
        user.setIsDel("Y");
        user.setUpdateTime(new Date());
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public void resetPassword(User user){
        userMapper.resetPassword(user);
    }

}
